package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public void addBed(BedDto bedDto) {
        Bed newBed = BeanUtil.copyProperties(bedDto, Bed.class);
        newBed.setBedStatus(0);
        try {
            bedMapper.InsertBed(newBed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    @Override
    public void deleteBedById(Long id) {

        bedMapper.deleteBedById(id);
    }

    @Override
    public BedVo readBedById(Long id) {
        Bed bed = bedMapper.selectBedById(id);
        BedVo bedVo = BeanUtil.copyProperties(bed, BedVo.class);
        return bedVo;
    }

    @Override
    public void updateBed(BedDto bedDto) {
        Bed updateBed = BeanUtil.copyProperties(bedDto, Bed.class);
        updateBed.setBedStatus(0);
        updateBed.setUpdateTime(LocalDateTime.now());
        bedMapper.updateBed(updateBed);

    }
}

